/**
 * 
 */
package com.hfjyz.exam.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.exam.command.ExamCommand;
import com.hfjyz.exam.command.ExamResultCommand;
import com.hfjyz.exam.command.PaperListCommand;
import com.hfjyz.exam.contants.ExamContants;
import com.hfjyz.exam.dao.ExamDetailDao;
import com.hfjyz.exam.dao.ExamInfoDao;
import com.hfjyz.exam.dao.PaperDao;
import com.hfjyz.exam.model.ExamDetail;
import com.hfjyz.exam.model.ExamInfo;
import com.hfjyz.exam.model.Paper;
import com.hfjyz.exam.model.PaperDetail;
import com.hfjyz.exam.model.PaperSection;
import com.hfjyz.exam.model.UserInfo;

/**
 *
 */
public class PaperServiceImpl extends AbstractServiceImpl implements
		PaperService {
	private PaperDao paperDao;
	private ExamInfoDao examInfoDao;
	private ExamDetailDao examDetailDao;

	@SuppressWarnings("rawtypes")
	@Override
	public Map initSearch(HttpServletRequest request, PaperListCommand cmd,
			Errors errors) throws Exception {
		List<Paper> papers = paperDao.find(cmd);
		cmd.setResult(papers);
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map initExam(HttpServletRequest request, ExamCommand cmd,
			Errors errors) throws Exception {

		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_LOGIN_INFO);
		ExamInfo examInfo = examInfoDao
				.getByUid(userInfo.getId(), cmd.getPid());
		if (examInfo != null) {
			throw new Exception("您已经参加过本次考试。可以查看您的成绩。");
		}
		Paper p = paperDao.getForExam(cmd.getPid());
		cmd.setPaper(p);
		if (cmd.getStartTime() == null || cmd.getUid() == null
				|| userInfo.getId() != cmd.getUid()) {
			cmd.setStartTime(Calendar.getInstance().getTime());
		}
		cmd.setUid(userInfo.getId());
		return null;
	}

	@Transactional
	@Override
	public void exam(HttpServletRequest request, ExamCommand cmd,
			BindException errors) throws Exception {

		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_LOGIN_INFO);
		List<PaperSection> pss = cmd.getPaper().getPaperSections();
		List<ExamDetail> eds = new ArrayList<ExamDetail>();

		Integer total = 0;
		for (PaperSection ps : pss) {
			for (PaperDetail pd : ps.getPaperDetails()) {
				ExamDetail ed = new ExamDetail();
				ed.setPaper(cmd.getPaper());
				ed.setQuestion(pd.getQuestion());
				ed.setUserInfo(userInfo);

				String answer = "";
				switch (pd.getQuestion().getQtype()) {
				case 1:
					// 判断题
				case 2:
					// 单选题
					answer = request.getParameter("question_"
							+ pd.getQuestion().getId());
				case 3:
					// 多选题
					String[] mchoice = request.getParameterValues("question_"
							+ pd.getQuestion().getId());
					answer = StringUtils.arrayToDelimitedString(mchoice, "");

				}

				ed.setAnswer(answer);
				if (answer != null && answer.equals(pd.getQuestion().getQkey())) {
					total += pd.getScore();
					ed.setScore(pd.getScore());
				} else {
					ed.setScore(0);
				}
				eds.add(ed);
			}
		}
		ExamInfo examInfo = new ExamInfo();
		examInfo.setEdate(Calendar.getInstance().getTime());
		examInfo.setSdate(cmd.getStartTime());
		examInfo.setIp(request.getRemoteAddr());
		examInfo.setPaper(cmd.getPaper());
		examInfo.setScore(total);
		examInfo.setEdate(Calendar.getInstance().getTime());
		examInfo.setUserInfo(userInfo);
		examInfoDao.add(examInfo);
		for (ExamDetail ed : eds) {
			examDetailDao.add(ed);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hfjyz.exam.service.PaperService#initExamResult(javax.servlet.http
	 * .HttpServletRequest, com.hfjyz.exam.command.ExamResultCommand,
	 * org.springframework.validation.Errors)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Map initExamResult(HttpServletRequest request,
			ExamResultCommand cmd, Errors errors) throws Exception {
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_LOGIN_INFO);
		List<ExamInfo> es = examInfoDao.findByUid(userInfo.getId());
		cmd.setExams(es);
		return null;
	}

	/**
	 * @return the paperDao
	 */
	public PaperDao getPaperDao() {
		return paperDao;
	}

	/**
	 * @param paperDao
	 *            the paperDao to set
	 */
	public void setPaperDao(PaperDao paperDao) {
		this.paperDao = paperDao;
	}

	/**
	 * @return the examDetailDao
	 */
	public ExamDetailDao getExamDetailDao() {
		return examDetailDao;
	}

	/**
	 * @param examDetailDao
	 *            the examDetailDao to set
	 */
	public void setExamDetailDao(ExamDetailDao examDetailDao) {
		this.examDetailDao = examDetailDao;
	}

	/**
	 * @return the examInfoDao
	 */
	public ExamInfoDao getExamInfoDao() {
		return examInfoDao;
	}

	/**
	 * @param examInfoDao
	 *            the examInfoDao to set
	 */
	public void setExamInfoDao(ExamInfoDao examInfoDao) {
		this.examInfoDao = examInfoDao;
	}

}
